<template>
  <div>
    <el-drawer
      v-if="drawer"
      :size="width"
      class="select-drawer"
      :show-close="true"
      :title="title"
      :visible.sync="visible"
      direction="rtl"
      :wrapper-closable="false"
      :close-on-press-escape="false"
      :destroy-on-close="false"
    >
      <slot />
      <div class="drawer-footer">
        <el-button size="mini" @click="cancel">
          {{ cancelText }}
        </el-button>
      </div>
    </el-drawer>
    <el-dialog
      v-else
      v-el-drag-dialog
      :custom-class="customClass"
      class="select-dialog"
      :title="title"
      :visible.sync="visible"
      :append-to-body="appendToBody"
      :show-close="true"
      :width="width"
      top="10vh"
      :before-close="cancel"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :destroy-on-close="false"
    >
      <slot />
      <div slot="footer">
        <el-button size="mini" @click="cancel">
          {{ cancelText }}
        </el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import elDragDialog from '../../directive/el-drag-dialog' // base on element-ui
export default {
  name: 'FormDialog',
  directives: { elDragDialog },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    customClass: {
      type: String,
      default: ''
    },
    title: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '60%'
    },
    appendToBody: {
      type: Boolean,
      default: false
    },
    cancelText: {
      type: String,
      default: '取消'
    },
    drawer: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
    }
  },
  computed: {
    visible: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    }
  },
  methods: {
    cancel() {
      this.$emit('on-cancel')
    }
  }
}
</script>
